package HeSuanServer.service.Impl;

import HeSuanServer.POJO.R.Result;
import HeSuanServer.POJO.R.StatusCode;
import HeSuanServer.POJO.User;
import HeSuanServer.POJO.UserSearch;
import HeSuanServer.mapper.UserMapper;
import HeSuanServer.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @描述:
 * @author: LH
 * @date: 2023/4/7
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public Result findByPage(UserSearch userSearch) {
        try{
            if (userSearch.getName()==null) {
                userSearch.setName("");
            }
            Map<String,Object> map=new HashMap<>();
            map.put("start",(userSearch.getCurrent()-1)* userSearch.getSize());
            map.put("end",8);
            map.put("p",userSearch.getName());
            List<User> list=userMapper.Search(map);
            Integer count=userMapper.Conut(map);
            Map<String,Object> data=new HashMap<>();
            data.put("list",list);
            data.put("total",count);
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(data);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    @Override
    public Result byId(Integer id) {
        try{
            User user=userMapper.byId(id);
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(user);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    @Override
    public Result edit(User user) {
        try {
            userMapper.update(user);
            return Result.Builder().StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_UPDATE);
        }
    }

    @Override
    public Result del(Integer id) {
        try {
            userMapper.del(id);
            return Result.Builder().StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_UPDATE);
        }
    }
}
